package store.lunangangster.engine.bpmn.parser.handler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import store.lunangangster.bpmn.model.BaseElement;
import store.lunangangster.engine.bpmn.parser.BpmnParser;

/**
 * {@link BpmnParseHandler}的聚合类
 *
 * @author cwk
 * @version 1.0
 */
public class BpmnParseHandlers {

  protected Map<Class<? extends BaseElement>, List<BpmnParseHandler>> bpmnHandlers = new HashMap<>();


  public void handleElement(BpmnParser bpmnParser, BaseElement baseElement) {
    List<BpmnParseHandler> handlers = bpmnHandlers.get(baseElement.getClass());
    handlers.forEach(handler -> {
      handler.handle(bpmnParser, baseElement);
    });
  }


  public void addHandlers(List<BpmnParseHandler> handlers) {
    for (BpmnParseHandler handler : handlers) {
      addHandler(handler);
    }
  }

  public void addHandler(BpmnParseHandler handler) {
    Class<? extends BaseElement> type = handler.getType();
    if (!bpmnHandlers.containsKey(type)) {
      bpmnHandlers.put(type, new ArrayList<>());
    }
    List<BpmnParseHandler> handlers = bpmnHandlers.get(type);
    handlers.add(handler);
  }
}
